﻿using Microsoft.AspNetCore.Mvc;
using Mysoft.AppStartup.Container.Filters;
using Mysoft.Common.Exceptions;
using Mysoft.Common.Response;
using Mysoft.Core.Filters;
using Mysoft.Data.CacheManager.IService;
using Mysoft.Data.UserManager;
using System.Net;

namespace Mysoft.App.Appservices.Basic
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController, JwtAuthorize,VerifyAuthorize]
    public class AppServiceBase: Controller
    {
        /// <summary>
        /// 无参构造
        /// </summary>
       public AppServiceBase() { 
        
        
        }
        /// <summary>
        /// 缓存服务
        /// </summary>
        protected internal ICacheService _CacheContext
        {
            get
            {
                return Mysoft.Common.Extensions.HttpContext.Current.RequestServices.GetService(typeof(ICacheService)) as ICacheService;
            }
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        protected internal UserContext _UserContext
        {
            get
            {
                return Mysoft.Common.Extensions.HttpContext.Current.RequestServices.GetService(typeof(UserContext)) as UserContext;
            }
        }

        /// <summary>
        /// 返回
        /// </summary>
        /// <returns></returns>
        protected internal ResponseContent Response(ResponseContent result, object data, bool success, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            result.code = (int)statusCode;
            result.success = success;
            if (success)
            {
                result.data = data;
            }
            else {
                result.message = data.ToString();
            }
            return result;
        }

        /// <summary>
        /// 成功返回
        /// </summary>
        /// <returns></returns>
        protected internal JsonResult Success(object data, HttpStatusCode statusCode = HttpStatusCode.OK) 
        {
            SuccessResponse result = new SuccessResponse();
            result.code = (int)statusCode;
            result.success = true;
            result.data = data;;
            return Json(result);
        }
        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="message"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        protected internal JsonResult Error(string message, HttpStatusCode statusCode = HttpStatusCode.BadRequest)
        {
            ErrorResponse result = new ErrorResponse();
            result.code = (int)statusCode;
            result.success = false;
            result.message = message;
            return Json(result);
        }
       /// <summary>
       /// 抛出异常
       /// </summary>
       /// <param name="message"></param>
       /// <param name="statusCode"></param>
       /// <returns></returns>
        protected internal JsonResult BusinessException(string message)
        {
            throw new BusinessException(message);
        }
      
    }
}
